


    /*
     * Check for browser support
     */
    var supportMsg = document.getElementById('msg');
    
    if ('speechSynthesis' in window) {
      supportMsg.innerHTML = '您的浏览器 <strong>支持</strong> 语音服务.';
    } else {
      supportMsg.innerHTML = '抱歉， 您的浏览器 <strong>不支持</strong> 语音服务.<br>请尝试使用Chrome浏览器.';
    //   <br>Try this in <a href="https://www.google.co.uk/intl/en/chrome/browser/canary.html">Chrome Canary</a>
      supportMsg.classList.add('not-supported');
    }
    
    
    // Get the 'speak' button
    var button = document.getElementById('speak');
    
    // Get the text input element.
    var speechMsgInput = document.getElementById('speech-msg');
    
    // Get the voice select element.
    var voiceSelect = document.getElementById('voice');
    
    // Get the attribute controls.
    var volumeInput = document.getElementById('volume');
    var rateInput = document.getElementById('rate');
    var pitchInput = document.getElementById('pitch');
    
    
    // Fetch the list of voices and populate the voice options.
    function loadVoices() {
      // Fetch the available voices.
      var voices = speechSynthesis.getVoices();
      
      // Loop through each of the voices.
      voices.forEach(function(voice, i) {
        // Create a new option element.
        var option = document.createElement('option');
        
        // Set the options value and text.
        option.value = voice.name;
        option.innerHTML = voice.name;
          
        // Add the option to the voice selector.
        voiceSelect.appendChild(option);
      });
    }
    
    // Execute loadVoices.
    loadVoices();
    
    // Chrome loads voices asynchronously.
    window.speechSynthesis.onvoiceschanged = function(e) {
      loadVoices();
    };
    
    
    // Create a new utterance for the specified text and add it to
    // the queue.
    // Create a new instance of SpeechSynthesisUtterance.
    var msg = new SpeechSynthesisUtterance();
    function speak(text) {
      msg.text = text;     
      // Set the attributes.
      msg.volume = parseFloat(volumeInput.value);
      msg.rate = parseFloat(rateInput.value);
      msg.pitch = parseFloat(pitchInput.value);
      
      // If a voice has been selected, find the voice and set the
      // utterance instance's voice attribute.
      if (voiceSelect.value) {
        msg.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == voiceSelect.value; })[0];
      }
      
      // Queue this utterance.
      window.speechSynthesis.cancel();
      window.speechSynthesis.speak(msg);
    }
    
    //朗读全文
    // Set up an event listener for when the 'speak' button is clicked.
    button.addEventListener('click', function(e) {
        let text = document.querySelector('.content').textContent
        speak(text);
    });

    //展开和折叠面板
    document.querySelector("div#switch").addEventListener('click',()=>{
      document.querySelector("div#control").classList.toggle('hidden')
    })

    document.addEventListener('mouseup',()=>{
      if (window.getSelection) {
        let text = window.getSelection().toString()
        speak(text);
      }
    })